
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  
<!-- Mirrored from werkzeug.palletsprojects.com/en/1.0.x/utils/ by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 15 Sep 2020 06:37:09 GMT -->
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Utilities &#8212; Werkzeug Documentation (1.0.x)</title>
    <link rel="stylesheet" href="../_static/werkzeug.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../../../../assets.readthedocs.org/static/css/badge_only.css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script async="async" type="text/javascript" src="../../../../assets.readthedocs.org/static/javascript/readthedocs-doc-embed.js"></script>
    <link rel="shortcut icon" href="../_static/favicon.ico"/>
    <link rel="index" title="Index" href="../genindex/index.html" />
    <link rel="search" title="Search" href="../search/index.html" />
    <link rel="next" title="URL Helpers" href="../urls/index.html" />
    <link rel="prev" title="Data Structures" href="../datastructures/index.html" />
    <link rel="canonical" href="index.html">
  <script>DOCUMENTATION_OPTIONS.URL_ROOT = '../index.html';</script>
   
  
<!-- RTD Extra Head -->

<!-- 
Always link to the latest version, as canonical.
http://docs.readthedocs.org/en/latest/canonical.html
-->
<link rel="canonical" href="index.html" />

<link rel="stylesheet" href="../../../../assets.readthedocs.org/static/css/readthedocs-doc-embed.css" type="text/css" />

<script type="text/javascript" src="../_static/readthedocs-data.js"></script>

<!-- Add page-specific data, which must exist in the page js, not global -->
<script type="text/javascript">
READTHEDOCS_DATA['page'] = "utils"
READTHEDOCS_DATA['source_suffix'] = ".rst"
</script>

<script type="text/javascript" src="../../../../assets.readthedocs.org/static/javascript/readthedocs-analytics.js" async="async"></script>

<!-- end RTD <extrahead> -->
</head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex/index.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex/index.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../urls/index.html" title="URL Helpers"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="../datastructures/index.html" title="Data Structures"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">Werkzeug Documentation (1.0.x)</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="utilities">
<h1>Utilities<a class="headerlink" href="#utilities" title="Permalink to this headline">¶</a></h1>
<p>Various utility functions shipped with Werkzeug.</p>
<div class="section" id="module-werkzeug.utils">
<span id="html-helpers"></span><h2>HTML Helpers<a class="headerlink" href="#module-werkzeug.utils" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="werkzeug.utils.HTMLBuilder">
<em class="property">class </em><code class="descclassname">werkzeug.utils.</code><code class="descname">HTMLBuilder</code><span class="sig-paren">(</span><em>dialect</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.HTMLBuilder" title="Permalink to this definition">¶</a></dt>
<dd><p>Helper object for HTML generation.</p>
<p>Per default there are two instances of that class.  The <cite>html</cite> one, and
the <cite>xhtml</cite> one for those two dialects.  The class uses keyword parameters
and positional parameters to generate small snippets of HTML.</p>
<p>Keyword parameters are converted to XML/SGML attributes, positional
arguments are used as children.  Because Python accepts positional
arguments before keyword arguments it’s a good idea to use a list with the
star-syntax for some children:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">html</span><span class="o">.</span><span class="n">p</span><span class="p">(</span><span class="n">class_</span><span class="o">=</span><span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="o">*</span><span class="p">[</span><span class="n">html</span><span class="o">.</span><span class="n">a</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="n">href</span><span class="o">=</span><span class="s1">&#39;foo.html&#39;</span><span class="p">),</span> <span class="s1">&#39; &#39;</span><span class="p">,</span>
<span class="gp">... </span>                       <span class="n">html</span><span class="o">.</span><span class="n">a</span><span class="p">(</span><span class="s1">&#39;bar&#39;</span><span class="p">,</span> <span class="n">href</span><span class="o">=</span><span class="s1">&#39;bar.html&#39;</span><span class="p">)])</span>
<span class="go">u&#39;&lt;p class=&quot;foo&quot;&gt;&lt;a href=&quot;foo.html&quot;&gt;foo&lt;/a&gt; &lt;a href=&quot;bar.html&quot;&gt;bar&lt;/a&gt;&lt;/p&gt;&#39;</span>
</pre></div>
</div>
<p>This class works around some browser limitations and can not be used for
arbitrary SGML/XML generation.  For that purpose lxml and similar
libraries exist.</p>
<p>Calling the builder escapes the string passed:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">html</span><span class="o">.</span><span class="n">p</span><span class="p">(</span><span class="n">html</span><span class="p">(</span><span class="s2">&quot;&lt;foo&gt;&quot;</span><span class="p">))</span>
<span class="go">u&#39;&lt;p&gt;&amp;lt;foo&amp;gt;&lt;/p&gt;&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="werkzeug.utils.escape">
<code class="descclassname">werkzeug.utils.</code><code class="descname">escape</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.escape" title="Permalink to this definition">¶</a></dt>
<dd><p>Replace special characters “&amp;”, “&lt;”, “&gt;” and (“) to HTML-safe sequences.</p>
<p>There is a special handling for <cite>None</cite> which escapes to an empty string.</p>
<details class="changelog">
<summary>Changelog</summary><div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.9: </span><cite>quote</cite> is now implicitly on.</p>
</div>
</details><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>s</strong> – the string to escape.</li>
<li><strong>quote</strong> – ignored.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="werkzeug.utils.unescape">
<code class="descclassname">werkzeug.utils.</code><code class="descname">unescape</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.unescape" title="Permalink to this definition">¶</a></dt>
<dd><p>The reverse function of <cite>escape</cite>.  This unescapes all the HTML
entities, not only the XML entities inserted by <cite>escape</cite>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>s</strong> – the string to unescape.</td>
</tr>
</tbody>
</table>
</dd></dl>

</div>
<div class="section" id="general-helpers">
<h2>General Helpers<a class="headerlink" href="#general-helpers" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="werkzeug.utils.cached_property">
<em class="property">class </em><code class="descclassname">werkzeug.utils.</code><code class="descname">cached_property</code><span class="sig-paren">(</span><em>func</em>, <em>name=None</em>, <em>doc=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.cached_property" title="Permalink to this definition">¶</a></dt>
<dd><p>A decorator that converts a function into a lazy property.  The
function wrapped is called the first time to retrieve the result
and then that calculated result is used the next time you access
the value:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Foo</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># calculate something important here</span>
        <span class="k">return</span> <span class="mi">42</span>
</pre></div>
</div>
<p>The class has to have a <cite>__dict__</cite> in order for this property to
work.</p>
</dd></dl>

<dl class="function">
<dt id="werkzeug.utils.invalidate_cached_property">
<code class="descclassname">werkzeug.utils.</code><code class="descname">invalidate_cached_property</code><span class="sig-paren">(</span><em>obj</em>, <em>name</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.invalidate_cached_property" title="Permalink to this definition">¶</a></dt>
<dd><p>Invalidates the cache for a <a class="reference internal" href="#werkzeug.utils.cached_property" title="werkzeug.utils.cached_property"><code class="xref py py-class docutils literal notranslate"><span class="pre">cached_property</span></code></a>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Test</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="nd">@cached_property</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">magic_number</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;recalculating...&quot;</span><span class="p">)</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="mi">42</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">var</span> <span class="o">=</span> <span class="n">Test</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">var</span><span class="o">.</span><span class="n">magic_number</span>
<span class="go">recalculating...</span>
<span class="go">42</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">var</span><span class="o">.</span><span class="n">magic_number</span>
<span class="go">42</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">invalidate_cached_property</span><span class="p">(</span><span class="n">var</span><span class="p">,</span> <span class="s2">&quot;magic_number&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">var</span><span class="o">.</span><span class="n">magic_number</span>
<span class="go">recalculating...</span>
<span class="go">42</span>
</pre></div>
</div>
<p>You must pass the name of the cached property as the second argument.</p>
</dd></dl>

<dl class="class">
<dt id="werkzeug.utils.environ_property">
<em class="property">class </em><code class="descclassname">werkzeug.utils.</code><code class="descname">environ_property</code><span class="sig-paren">(</span><em>name</em>, <em>default=None</em>, <em>load_func=None</em>, <em>dump_func=None</em>, <em>read_only=None</em>, <em>doc=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.environ_property" title="Permalink to this definition">¶</a></dt>
<dd><p>Maps request attributes to environment variables. This works not only
for the Werzeug request object, but also any other class with an
environ attribute:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Test</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">environ</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;key&#39;</span><span class="p">:</span> <span class="s1">&#39;value&#39;</span><span class="p">}</span>
<span class="gp">... </span>    <span class="n">test</span> <span class="o">=</span> <span class="n">environ_property</span><span class="p">(</span><span class="s1">&#39;key&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">var</span> <span class="o">=</span> <span class="n">Test</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">var</span><span class="o">.</span><span class="n">test</span>
<span class="go">&#39;value&#39;</span>
</pre></div>
</div>
<p>If you pass it a second value it’s used as default if the key does not
exist, the third one can be a converter that takes a value and converts
it.  If it raises <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#ValueError" title="(in Python v3.8)"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> or <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#TypeError" title="(in Python v3.8)"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> the default value
is used. If no default value is provided <cite>None</cite> is used.</p>
<p>Per default the property is read only.  You have to explicitly enable it
by passing <code class="docutils literal notranslate"><span class="pre">read_only=False</span></code> to the constructor.</p>
</dd></dl>

<dl class="class">
<dt id="werkzeug.utils.header_property">
<em class="property">class </em><code class="descclassname">werkzeug.utils.</code><code class="descname">header_property</code><span class="sig-paren">(</span><em>name</em>, <em>default=None</em>, <em>load_func=None</em>, <em>dump_func=None</em>, <em>read_only=None</em>, <em>doc=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.header_property" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <cite>environ_property</cite> but for headers.</p>
</dd></dl>

<dl class="function">
<dt id="werkzeug.utils.redirect">
<code class="descclassname">werkzeug.utils.</code><code class="descname">redirect</code><span class="sig-paren">(</span><em>location</em>, <em>code=302</em>, <em>Response=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.redirect" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a response object (a WSGI application) that, if called,
redirects the client to the target location. Supported codes are
301, 302, 303, 305, 307, and 308. 300 is not supported because
it’s not a real redirect and 304 because it’s the answer for a
request with a request with defined If-Modified-Since headers.</p>
<details class="changelog">
<summary>Changelog</summary><div class="versionadded">
<p><span class="versionmodified">New in version 0.10: </span>The class used for the Response object can now be passed in.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.6: </span>The location can now be a unicode string that is encoded using
the <code class="xref py py-func docutils literal notranslate"><span class="pre">iri_to_uri()</span></code> function.</p>
</div>
</details><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>location</strong> – the location the response should redirect to.</li>
<li><strong>code</strong> – the redirect status code. defaults to 302.</li>
<li><strong>Response</strong> (<em>class</em>) – a Response class to use when instantiating a
response. The default is <a class="reference internal" href="../wrappers/index.html#werkzeug.wrappers.Response" title="werkzeug.wrappers.Response"><code class="xref py py-class docutils literal notranslate"><span class="pre">werkzeug.wrappers.Response</span></code></a> if
unspecified.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="werkzeug.utils.append_slash_redirect">
<code class="descclassname">werkzeug.utils.</code><code class="descname">append_slash_redirect</code><span class="sig-paren">(</span><em>environ</em>, <em>code=301</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.append_slash_redirect" title="Permalink to this definition">¶</a></dt>
<dd><p>Redirects to the same URL but with a slash appended.  The behavior
of this function is undefined if the path ends with a slash already.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>environ</strong> – the WSGI environment for the request that triggers
the redirect.</li>
<li><strong>code</strong> – the status code for the redirect.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="werkzeug.utils.import_string">
<code class="descclassname">werkzeug.utils.</code><code class="descname">import_string</code><span class="sig-paren">(</span><em>import_name</em>, <em>silent=False</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.import_string" title="Permalink to this definition">¶</a></dt>
<dd><p>Imports an object based on a string.  This is useful if you want to
use import paths as endpoints or something similar.  An import path can
be specified either in dotted notation (<code class="docutils literal notranslate"><span class="pre">xml.sax.saxutils.escape</span></code>)
or with a colon as object delimiter (<code class="docutils literal notranslate"><span class="pre">xml.sax.saxutils:escape</span></code>).</p>
<p>If <cite>silent</cite> is True the return value will be <cite>None</cite> if the import fails.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>import_name</strong> – the dotted name for the object to import.</li>
<li><strong>silent</strong> – if set to <cite>True</cite> import errors are ignored and
<cite>None</cite> is returned instead.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">imported object</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="werkzeug.utils.find_modules">
<code class="descclassname">werkzeug.utils.</code><code class="descname">find_modules</code><span class="sig-paren">(</span><em>import_path</em>, <em>include_packages=False</em>, <em>recursive=False</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.find_modules" title="Permalink to this definition">¶</a></dt>
<dd><p>Finds all the modules below a package.  This can be useful to
automatically import all views / controllers so that their metaclasses /
function decorators have a chance to register themselves on the
application.</p>
<p>Packages are not returned unless <cite>include_packages</cite> is <cite>True</cite>.  This can
also recursively list modules but in that case it will import all the
packages to get the correct load path of that module.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>import_path</strong> – the dotted name for the package to find child modules.</li>
<li><strong>include_packages</strong> – set to <cite>True</cite> if packages should be returned, too.</li>
<li><strong>recursive</strong> – set to <cite>True</cite> if recursion should happen.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">generator</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="werkzeug.utils.validate_arguments">
<code class="descclassname">werkzeug.utils.</code><code class="descname">validate_arguments</code><span class="sig-paren">(</span><em>func</em>, <em>args</em>, <em>kwargs</em>, <em>drop_extra=True</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.validate_arguments" title="Permalink to this definition">¶</a></dt>
<dd><p>Checks if the function accepts the arguments and keyword arguments.
Returns a new <code class="docutils literal notranslate"><span class="pre">(args,</span> <span class="pre">kwargs)</span></code> tuple that can safely be passed to
the function without causing a <cite>TypeError</cite> because the function signature
is incompatible.  If <cite>drop_extra</cite> is set to <cite>True</cite> (which is the default)
any extra positional or keyword arguments are dropped automatically.</p>
<p>The exception raised provides three attributes:</p>
<dl class="docutils">
<dt><cite>missing</cite></dt>
<dd>A set of argument names that the function expected but where
missing.</dd>
<dt><cite>extra</cite></dt>
<dd>A dict of keyword arguments that the function can not handle but
where provided.</dd>
<dt><cite>extra_positional</cite></dt>
<dd>A list of values that where given by positional argument but the
function cannot accept.</dd>
</dl>
<p>This can be useful for decorators that forward user submitted data to
a view function:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">werkzeug.utils</span> <span class="k">import</span> <span class="n">ArgumentValidationError</span><span class="p">,</span> <span class="n">validate_arguments</span>

<span class="k">def</span> <span class="nf">sanitize</span><span class="p">(</span><span class="n">f</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">proxy</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">to_dict</span><span class="p">()</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span> <span class="o">=</span> <span class="n">validate_arguments</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="p">(</span><span class="n">request</span><span class="p">,),</span> <span class="n">data</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">ArgumentValidationError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">BadRequest</span><span class="p">(</span><span class="s1">&#39;The browser failed to transmit all &#39;</span>
                             <span class="s1">&#39;the data expected.&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">f</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">proxy</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>func</strong> – the function the validation is performed against.</li>
<li><strong>args</strong> – a tuple of positional arguments.</li>
<li><strong>kwargs</strong> – a dict of keyword arguments.</li>
<li><strong>drop_extra</strong> – set to <cite>False</cite> if you don’t want extra arguments
to be silently dropped.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">tuple in the form <code class="docutils literal notranslate"><span class="pre">(args,</span> <span class="pre">kwargs)</span></code>.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="werkzeug.utils.secure_filename">
<code class="descclassname">werkzeug.utils.</code><code class="descname">secure_filename</code><span class="sig-paren">(</span><em>filename</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.secure_filename" title="Permalink to this definition">¶</a></dt>
<dd><p>Pass it a filename and it will return a secure version of it.  This
filename can then safely be stored on a regular file system and passed
to <a class="reference external" href="https://docs.python.org/3/library/os.path.html#os.path.join" title="(in Python v3.8)"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.join()</span></code></a>.  The filename returned is an ASCII only string
for maximum portability.</p>
<p>On windows systems the function also makes sure that the file is not
named after one of the special device files.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">secure_filename</span><span class="p">(</span><span class="s2">&quot;My cool movie.mov&quot;</span><span class="p">)</span>
<span class="go">&#39;My_cool_movie.mov&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">secure_filename</span><span class="p">(</span><span class="s2">&quot;../../../etc/passwd&quot;</span><span class="p">)</span>
<span class="go">&#39;etc_passwd&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">secure_filename</span><span class="p">(</span><span class="sa">u</span><span class="s1">&#39;i contain cool </span><span class="se">\xfc</span><span class="s1">ml</span><span class="se">\xe4</span><span class="s1">uts.txt&#39;</span><span class="p">)</span>
<span class="go">&#39;i_contain_cool_umlauts.txt&#39;</span>
</pre></div>
</div>
<p>The function might return an empty filename.  It’s your responsibility
to ensure that the filename is unique and that you abort or
generate a random filename if the function returned an empty one.</p>
<details class="changelog">
<summary>Changelog</summary><div class="versionadded">
<p><span class="versionmodified">New in version 0.5.</span></p>
</div>
</details><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>filename</strong> – the filename to secure</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="werkzeug.utils.bind_arguments">
<code class="descclassname">werkzeug.utils.</code><code class="descname">bind_arguments</code><span class="sig-paren">(</span><em>func</em>, <em>args</em>, <em>kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.bind_arguments" title="Permalink to this definition">¶</a></dt>
<dd><p>Bind the arguments provided into a dict.  When passed a function,
a tuple of arguments and a dict of keyword arguments <cite>bind_arguments</cite>
returns a dict of names as the function would see it.  This can be useful
to implement a cache decorator that uses the function arguments to build
the cache key based on the values of the arguments.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>func</strong> – the function the arguments should be bound for.</li>
<li><strong>args</strong> – tuple of positional arguments.</li>
<li><strong>kwargs</strong> – a dict of keyword arguments.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">a <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> of bound keyword arguments.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>

</div>
<div class="section" id="url-helpers">
<h2>URL Helpers<a class="headerlink" href="#url-helpers" title="Permalink to this headline">¶</a></h2>
<p>Please refer to <a class="reference internal" href="../urls/index.html"><span class="doc">URL Helpers</span></a>.</p>
</div>
<div class="section" id="module-werkzeug.useragents">
<span id="useragent-parsing"></span><h2>UserAgent Parsing<a class="headerlink" href="#module-werkzeug.useragents" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="werkzeug.useragents.UserAgent">
<em class="property">class </em><code class="descclassname">werkzeug.useragents.</code><code class="descname">UserAgent</code><span class="sig-paren">(</span><em>environ_or_string</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.useragents.UserAgent" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a user agent.  Pass it a WSGI environment or a user agent
string and you can inspect some of the details from the user agent
string via the attributes.  The following attributes exist:</p>
<dl class="attribute">
<dt id="werkzeug.useragents.UserAgent.string">
<code class="descname">string</code><a class="headerlink" href="#werkzeug.useragents.UserAgent.string" title="Permalink to this definition">¶</a></dt>
<dd><p>the raw user agent string</p>
</dd></dl>

<dl class="attribute">
<dt id="werkzeug.useragents.UserAgent.platform">
<code class="descname">platform</code><a class="headerlink" href="#werkzeug.useragents.UserAgent.platform" title="Permalink to this definition">¶</a></dt>
<dd><p>the browser platform. <code class="docutils literal notranslate"><span class="pre">None</span></code> if not recognized.
The following platforms are currently recognized:</p>
<ul class="simple">
<li><cite>aix</cite></li>
<li><cite>amiga</cite></li>
<li><cite>android</cite></li>
<li><cite>blackberry</cite></li>
<li><cite>bsd</cite></li>
<li><cite>chromeos</cite></li>
<li><cite>dragonflybsd</cite></li>
<li><cite>freebsd</cite></li>
<li><cite>hpux</cite></li>
<li><cite>ipad</cite></li>
<li><cite>iphone</cite></li>
<li><cite>irix</cite></li>
<li><cite>linux</cite></li>
<li><cite>macos</cite></li>
<li><cite>netbsd</cite></li>
<li><cite>openbsd</cite></li>
<li><cite>sco</cite></li>
<li><cite>solaris</cite></li>
<li><cite>symbian</cite></li>
<li><cite>wii</cite></li>
<li><cite>windows</cite></li>
</ul>
</dd></dl>

<dl class="attribute">
<dt id="werkzeug.useragents.UserAgent.browser">
<code class="descname">browser</code><a class="headerlink" href="#werkzeug.useragents.UserAgent.browser" title="Permalink to this definition">¶</a></dt>
<dd><p>the name of the browser. <code class="docutils literal notranslate"><span class="pre">None</span></code> if not recognized.
The following browsers are currently recognized:</p>
<ul class="simple">
<li><cite>aol</cite> *</li>
<li><cite>ask</cite> *</li>
<li><cite>baidu</cite> *</li>
<li><cite>bing</cite> *</li>
<li><cite>camino</cite></li>
<li><cite>chrome</cite></li>
<li><cite>edge</cite></li>
<li><cite>firefox</cite></li>
<li><cite>galeon</cite></li>
<li><cite>google</cite> *</li>
<li><cite>kmeleon</cite></li>
<li><cite>konqueror</cite></li>
<li><cite>links</cite></li>
<li><cite>lynx</cite></li>
<li><cite>mozilla</cite></li>
<li><cite>msie</cite></li>
<li><cite>msn</cite></li>
<li><cite>netscape</cite></li>
<li><cite>opera</cite></li>
<li><cite>safari</cite></li>
<li><cite>seamonkey</cite></li>
<li><cite>webkit</cite></li>
<li><cite>yahoo</cite> *</li>
</ul>
<p>(Browsers marked with a star (<code class="docutils literal notranslate"><span class="pre">*</span></code>) are crawlers.)</p>
</dd></dl>

<dl class="attribute">
<dt id="werkzeug.useragents.UserAgent.version">
<code class="descname">version</code><a class="headerlink" href="#werkzeug.useragents.UserAgent.version" title="Permalink to this definition">¶</a></dt>
<dd><p>the version of the browser. <code class="docutils literal notranslate"><span class="pre">None</span></code> if not recognized.</p>
</dd></dl>

<dl class="attribute">
<dt id="werkzeug.useragents.UserAgent.language">
<code class="descname">language</code><a class="headerlink" href="#werkzeug.useragents.UserAgent.language" title="Permalink to this definition">¶</a></dt>
<dd><p>the language of the browser. <code class="docutils literal notranslate"><span class="pre">None</span></code> if not recognized.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="module-werkzeug.security">
<span id="security-helpers"></span><h2>Security Helpers<a class="headerlink" href="#module-werkzeug.security" title="Permalink to this headline">¶</a></h2>
<details class="changelog">
<summary>Changelog</summary><div class="versionadded">
<p><span class="versionmodified">New in version 0.6.1.</span></p>
</div>
</details><dl class="function">
<dt id="werkzeug.security.generate_password_hash">
<code class="descclassname">werkzeug.security.</code><code class="descname">generate_password_hash</code><span class="sig-paren">(</span><em>password</em>, <em>method='pbkdf2:sha256'</em>, <em>salt_length=8</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.security.generate_password_hash" title="Permalink to this definition">¶</a></dt>
<dd><p>Hash a password with the given method and salt with a string of
the given length. The format of the string returned includes the method
that was used so that <a class="reference internal" href="#werkzeug.security.check_password_hash" title="werkzeug.security.check_password_hash"><code class="xref py py-func docutils literal notranslate"><span class="pre">check_password_hash()</span></code></a> can check the hash.</p>
<p>The format for the hashed string looks like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>method$salt$hash
</pre></div>
</div>
<p>This method can <strong>not</strong> generate unsalted passwords but it is possible
to set param method=’plain’ in order to enforce plaintext passwords.
If a salt is used, hmac is used internally to salt the password.</p>
<p>If PBKDF2 is wanted it can be enabled by setting the method to
<code class="docutils literal notranslate"><span class="pre">pbkdf2:method:iterations</span></code> where iterations is optional:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>pbkdf2:sha256:80000$salt$hash
pbkdf2:sha256$salt$hash
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>password</strong> – the password to hash.</li>
<li><strong>method</strong> – the hash method to use (one that hashlib supports). Can
optionally be in the format <code class="docutils literal notranslate"><span class="pre">pbkdf2:&lt;method&gt;[:iterations]</span></code>
to enable PBKDF2.</li>
<li><strong>salt_length</strong> – the length of the salt in letters.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="werkzeug.security.check_password_hash">
<code class="descclassname">werkzeug.security.</code><code class="descname">check_password_hash</code><span class="sig-paren">(</span><em>pwhash</em>, <em>password</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.security.check_password_hash" title="Permalink to this definition">¶</a></dt>
<dd><p>check a password against a given salted and hashed password value.
In order to support unsalted legacy passwords this method supports
plain text passwords, md5 and sha1 hashes (both salted and unsalted).</p>
<p>Returns <cite>True</cite> if the password matched, <cite>False</cite> otherwise.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>pwhash</strong> – a hashed string like returned by
<a class="reference internal" href="#werkzeug.security.generate_password_hash" title="werkzeug.security.generate_password_hash"><code class="xref py py-func docutils literal notranslate"><span class="pre">generate_password_hash()</span></code></a>.</li>
<li><strong>password</strong> – the plaintext password to compare against the hash.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="werkzeug.security.safe_str_cmp">
<code class="descclassname">werkzeug.security.</code><code class="descname">safe_str_cmp</code><span class="sig-paren">(</span><em>a</em>, <em>b</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.security.safe_str_cmp" title="Permalink to this definition">¶</a></dt>
<dd><p>This function compares strings in somewhat constant time.  This
requires that the length of at least one string is known in advance.</p>
<p>Returns <cite>True</cite> if the two strings are equal, or <cite>False</cite> if they are not.</p>
<details class="changelog">
<summary>Changelog</summary><div class="versionadded">
<p><span class="versionmodified">New in version 0.7.</span></p>
</div>
</details></dd></dl>

<dl class="function">
<dt id="werkzeug.security.safe_join">
<code class="descclassname">werkzeug.security.</code><code class="descname">safe_join</code><span class="sig-paren">(</span><em>directory</em>, <em>*pathnames</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.security.safe_join" title="Permalink to this definition">¶</a></dt>
<dd><p>Safely join zero or more untrusted path components to a base
directory to avoid escaping the base directory.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>directory</strong> – The trusted base directory.</li>
<li><strong>pathnames</strong> – The untrusted path components relative to the
base directory.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">A safe path, otherwise <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="werkzeug.security.pbkdf2_hex">
<code class="descclassname">werkzeug.security.</code><code class="descname">pbkdf2_hex</code><span class="sig-paren">(</span><em>data</em>, <em>salt</em>, <em>iterations=150000</em>, <em>keylen=None</em>, <em>hashfunc=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.security.pbkdf2_hex" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#werkzeug.security.pbkdf2_bin" title="werkzeug.security.pbkdf2_bin"><code class="xref py py-func docutils literal notranslate"><span class="pre">pbkdf2_bin()</span></code></a>, but returns a hex-encoded string.</p>
<details class="changelog">
<summary>Changelog</summary><div class="versionadded">
<p><span class="versionmodified">New in version 0.9.</span></p>
</div>
</details><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>data</strong> – the data to derive.</li>
<li><strong>salt</strong> – the salt for the derivation.</li>
<li><strong>iterations</strong> – the number of iterations.</li>
<li><strong>keylen</strong> – the length of the resulting key.  If not provided,
the digest size will be used.</li>
<li><strong>hashfunc</strong> – the hash function to use.  This can either be the
string name of a known hash function, or a function
from the hashlib module.  Defaults to sha256.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="werkzeug.security.pbkdf2_bin">
<code class="descclassname">werkzeug.security.</code><code class="descname">pbkdf2_bin</code><span class="sig-paren">(</span><em>data</em>, <em>salt</em>, <em>iterations=150000</em>, <em>keylen=None</em>, <em>hashfunc=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.security.pbkdf2_bin" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a binary digest for the PBKDF2 hash algorithm of <cite>data</cite>
with the given <cite>salt</cite>. It iterates <cite>iterations</cite> times and produces a
key of <cite>keylen</cite> bytes. By default, SHA-256 is used as hash function;
a different hashlib <cite>hashfunc</cite> can be provided.</p>
<details class="changelog">
<summary>Changelog</summary><div class="versionadded">
<p><span class="versionmodified">New in version 0.9.</span></p>
</div>
</details><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>data</strong> – the data to derive.</li>
<li><strong>salt</strong> – the salt for the derivation.</li>
<li><strong>iterations</strong> – the number of iterations.</li>
<li><strong>keylen</strong> – the length of the resulting key.  If not provided
the digest size will be used.</li>
<li><strong>hashfunc</strong> – the hash function to use.  This can either be the
string name of a known hash function or a function
from the hashlib module.  Defaults to sha256.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

</div>
<div class="section" id="logging">
<h2>Logging<a class="headerlink" href="#logging" title="Permalink to this headline">¶</a></h2>
<p>Werkzeug uses standard Python <a class="reference external" href="https://docs.python.org/3/library/logging.html#module-logging" title="(in Python v3.8)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code></a>. The logger is named
<code class="docutils literal notranslate"><span class="pre">&quot;werkzeug&quot;</span></code>.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">logging</span>
<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s2">&quot;werkzeug&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>If the logger level is not set, it will be set to <code class="xref py py-data docutils literal notranslate"><span class="pre">INFO</span></code>
on first use. If there is no handler for that level, a
<a class="reference external" href="https://docs.python.org/3/library/logging.handlers.html#logging.StreamHandler" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamHandler</span></code></a> is added.</p>
</div>
</div>


          </div>
        </div>
      </div>
  <span id="sidebar-top"></span>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  
    
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/werkzeug.png" alt="Logo"/>
            </a></p>
  

  <h3>Contents</h3>
  <ul>
<li><a class="reference internal" href="#">Utilities</a><ul>
<li><a class="reference internal" href="#module-werkzeug.utils">HTML Helpers</a></li>
<li><a class="reference internal" href="#general-helpers">General Helpers</a></li>
<li><a class="reference internal" href="#url-helpers">URL Helpers</a></li>
<li><a class="reference internal" href="#module-werkzeug.useragents">UserAgent Parsing</a></li>
<li><a class="reference internal" href="#module-werkzeug.security">Security Helpers</a></li>
<li><a class="reference internal" href="#logging">Logging</a></li>
</ul>
</li>
</ul>
<h3>Navigation</h3>
<ul>
  <li><a href="../index.html">Overview</a>
    <ul>
          <li>Previous: <a href="../datastructures/index.html" title="previous chapter">Data Structures</a>
          <li>Next: <a href="../urls/index.html" title="next chapter">URL Helpers</a>
    </ul>
  </li>
</ul>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://werkzeug.palletsprojects.com/en/1.0.x/search/" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
  
    <div class="footer" role="contentinfo">
        &#169; Copyright 2007 Pallets.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.8.5.
    </div>
    <script>
      if (typeof READTHEDOCS_DATA !== 'undefined') {
        if (!READTHEDOCS_DATA.features) {
          READTHEDOCS_DATA.features = {};
        }
        READTHEDOCS_DATA.features.docsearch_disabled = true;
      }
    </script>

  </body>

<!-- Mirrored from werkzeug.palletsprojects.com/en/1.0.x/utils/ by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 15 Sep 2020 06:37:09 GMT -->
</html>